package org.yt.control.common;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.yt.dao.common.LoginDao;
import org.yt.domain.Users;

public class LoginServlet extends BaseServlet {

	private static final long serialVersionUID = 1L;
	
	
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		LoginDao baseDao = new LoginDao();
		Users obj = new Users();
		String username = req.getParameter("username");
		String password = req.getParameter("password");
		obj.setUsername(username);
		obj.setPassword(password);
		List<Users> users = baseDao.checkUser(obj);
		RequestDispatcher dispatcher = null;
		if(users != null && users.size() == 1){
			req.getSession().setAttribute("users", users.get(0));
			dispatcher = req.getRequestDispatcher("views/common/index.jsp");
		}else{
			error = "用户不存在";
			req.setAttribute("error", error);
			dispatcher = req.getRequestDispatcher("views/common/error.jsp");
		}
		dispatcher.forward(req, res);
	}
	
}
